package com.msb.mall.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.msb.common.utils.PageUtils;
import com.msb.common.utils.R;
import com.msb.mall.product.entity.AttrAttrgroupRelationEntity;
import com.msb.mall.product.entity.AttrEntity;
import com.msb.mall.product.service.AttrAttrgroupRelationService;
import com.msb.mall.product.service.AttrGroupService;
import com.msb.mall.product.service.AttrService;
import com.msb.mall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * ??Ʒ?
 *
 * @author yujiayong
 * @email 1015086215@qq.com
 * @date 2025-03-05 12:45:48
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    private AttrGroupService attrGroupService;

    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }
    /**
     * 列表
     */
    @RequestMapping("/{attrtype}/list/{catelogId}")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params, Long catelogId, @PathVariable("attrtype") String attrtype){
        PageUtils page = attrService.queryPage(params, catelogId, attrtype);

        return R.ok().put("page", page);
    }
    @RequestMapping("/listNoQueryParam")
    //@RequiresPermissions("product:attr:list")
    public R listNoQueryParam(){
        List<AttrEntity> list = attrService.list();
        return R.ok().put("data", list);
    }

    @RequestMapping("/sale/list/{catelogId}")
    public R list(@PathVariable("catelogId") Long catelogId) {
        Map<String, Object> params = new HashMap<>();
        PageUtils page = attrService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		AttrEntity attr = attrService.getById(attrId);
        Long catelogId = attr.getCatelogId();
        Long[] categoryPath = categoryService.findCategoryPath(catelogId);
        attr.setCatelogPath(categoryPath);
        attr.setCatelogName(categoryService.getById(catelogId).getName());
        Long attrGroupId = attrAttrgroupRelationService.getOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId)).getAttrGroupId();
        String attrGroupName = attrGroupService.getById(attrGroupId).getAttrGroupName();
        attr.setAttrGroupName(attrGroupName);
        attr.setAttrGroupId(attrGroupId);
        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrEntity attr){
//		attrService.save(attr);
        attrService.saveDetails(attr);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrEntity attr){
//		attrService.updateById(attr);
        attrService.updateDetails(attr);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
